import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbcurriculums } from '../../../atoms/db';

const logger = anylogger('pages/api/attendence/query.api');
export type IData = ITbcurriculums

export type Result = {
	ok: true;
	data: IData[]
} | {
	ok: false;
	message: string;
};

export type Message = {
	course_id: string


}

/**
 * pages/api/attendence/query.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { course_id } = req.body as Message;
		if (!course_id) {
			res.status(200).json({
				ok: false,
				message: '错误'
			});
			return;
		}
		const d = await tbcurriculums()
			.select('*')
			.orderBy('course_id', 'asc');

		const data = d.map((it) => {
			return {
				...it
			} as IData;
		});

		res.status(200).json({
			ok: true,
			data
		});
	} catch (error) {
		logger.trace(error);
		res.status(200).json({
			ok: false,
			message: '错误'
		});
	}
});

export const config = {} as PageConfig;

export default handler;
